package com.x.crm.dao;

import com.x.crm.model.Cooperation;
import com.x.crm.model.VO.CooperationVO;
import com.x.crm.model.VO.IntentionVO;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class CooperationDao extends BaseDao{


	private final static String baseSelectCooperationListSql = "SELECT * FROM  CRM_COOPERATION  I " +
			"WHERE 1=1 ";

	public List<Cooperation> selectCooperationList(Cooperation cooperation) throws Exception {
		StringBuilder sb = new StringBuilder(baseSelectCooperationListSql);
		List<Object> params = new ArrayList<Object>();
		searchBasicCondition(cooperation, sb, params);
		sb.append(" ORDER BY I.CREATE_TIME DESC");
		return this.queryForList(sb.toString(), Cooperation.class, params, cooperation);
	}


	private final static String baseSelectCooperationNumSql = "SELECT COUNT(COOPER_ID) FROM  CRM_COOPERATION  I " +
			"WHERE 1=1 ";

	public int selectCooperationNum(Cooperation cooperation) throws Exception {
		StringBuilder sb = new StringBuilder(baseSelectCooperationNumSql);
		List<Object> params = new ArrayList<Object>();
		searchBasicCondition(cooperation, sb, params);
		return this.queryForInt(sb.toString(), params);
	}


	private void searchBasicCondition(Cooperation cooperation, StringBuilder sb, List<Object> params) {
		if (this.isString(cooperation.getCooperId())) {
			sb.append("AND I.COOPER_ID = ? ");
			params.add(cooperation.getCooperId());
		}
		if (this.isString(cooperation.getCoopertypeId())) {
			sb.append("AND I.COOPERTYPE_ID = ? ");
			params.add(cooperation.getCoopertypeId());
		}
		if (this.isNotNull(cooperation.getCooperState())) {
			sb.append("AND I.COOPER_STATE = ? ");
			params.add(cooperation.getCooperState());
		}
		if (cooperation.getCooperationVO() != null) {
			CooperationVO cooperationVO = cooperation.getCooperationVO();
			if (cooperationVO.getStartTime() != null) {
				sb.append("AND I.CREATE_TIME >= ? ");
				params.add(cooperationVO.getStartTime());
			}
			if (cooperationVO.getEndTime() != null) {
				sb.append("AND I.CREATE_TIME <= ? ");
				params.add(cooperationVO.getEndTime());
			}
		}
	}

}
